package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketNotifyConfigDao;
import com.bestcem.xm.ticket.dao.converter.TicketNotifyConfigConverter;
import com.bestcem.xm.ticket.dao.mapper.TicketNotifyConfigDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketNotifyConfig;
import com.bestcem.xm.ticket.entity.mysql.TicketNotifyConfigDO;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 11:46
 */
@Repository
public class TicketNotifyConfigDaoImpl implements TicketNotifyConfigDao {
    @Resource
    private TicketNotifyConfigDaoMapper ticketNotifyConfigDaoMapper;

    @Override
    public TicketNotifyConfig findById(String id) {
        TicketNotifyConfigDO ticketNotifyConfigDO = ticketNotifyConfigDaoMapper.findByTicketNotifyConfigId(id);

        return TicketNotifyConfigConverter.doToDto(ticketNotifyConfigDO);
    }

    @Override
    public List<TicketNotifyConfig> findAllByTicketConfigId(String ticketConfigId) {
        List<TicketNotifyConfigDO> ticketFlowDOList = ticketNotifyConfigDaoMapper.listByTicketConfigId(ticketConfigId);

        return TicketNotifyConfigConverter.doListToDtoList(ticketFlowDOList);
    }

    @Override
    public List<TicketNotifyConfig> findBySmtpIds(List<String> smtpIds) {
        List<TicketNotifyConfigDO> ticketFlowDOList = ticketNotifyConfigDaoMapper.listBySmtpIdList(smtpIds);

        return TicketNotifyConfigConverter.doListToDtoList(ticketFlowDOList);
    }

    @Override
    public void clearSmtpId(List<String> smtpIds) {
        ticketNotifyConfigDaoMapper.updateSmtpIdBySmtpIdList(smtpIds, DateUtil.getCommonDate());
    }

    @Override
    public List<TicketNotifyConfig> findAllByIds(List<String> ids) {
        List<TicketNotifyConfigDO> ticketNotifyConfigDOList = ticketNotifyConfigDaoMapper
                .listByTicketNotifyConfigIdList(ids);

        return TicketNotifyConfigConverter.doListToDtoList(ticketNotifyConfigDOList);
    }

    @Override
    public TicketNotifyConfig saveTicketNotifyConfig(TicketNotifyConfig ticketNotifyConfig) {
        TicketNotifyConfigDO entityDo = TicketNotifyConfigConverter.dtoToDo(ticketNotifyConfig);

        if (Objects.isNull(entityDo)) {
            entityDo = new TicketNotifyConfigDO();
        }
        if (ObjectUtils.isEmpty(entityDo.getTicketNotifyConfigId())) {
            entityDo.setTicketNotifyConfigId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getInstant())) {
            entityDo.setInstant(false);
        }
        if (Objects.isNull(entityDo.getStatus())) {
            entityDo.setStatus(0);
        }
        if (Objects.isNull(entityDo.getMode())) {
            entityDo.setMode(0);
        }
        if (Objects.isNull(entityDo.getEvent())) {
            entityDo.setEvent(0);
        }
        if (Objects.isNull(entityDo.getHoursLimit())) {
            entityDo.setHoursLimit(0);
        }
        if (Objects.isNull(entityDo.getHasReceiver())) {
            entityDo.setHasReceiver(false);
        }
        if (Objects.isNull(entityDo.getHasCcList())) {
            entityDo.setHasCcList(false);
        }

        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        ticketNotifyConfigDaoMapper.insert(entityDo);

        return TicketNotifyConfigConverter.doToDto(entityDo);
    }

    @Override
    public List<String> batchInsert(List<TicketNotifyConfig> ticketNotifyConfigs) {
        List<String> idList = new ArrayList<>();

        for (TicketNotifyConfig ticketNotifyConfig : ticketNotifyConfigs) {
            TicketNotifyConfigDO entityDo = TicketNotifyConfigConverter.dtoToDo(ticketNotifyConfig);
            if (Objects.isNull(entityDo)) {
                idList.add(null);
                continue;
            }
            entityDo.setTicketNotifyConfigId(ObjectId.next());
            if (Objects.isNull(entityDo.getCreateTime())) {
                entityDo.setCreateTime(DateUtil.getCommonDate());
            }
            if (Objects.isNull(entityDo.getUpdateTime())) {
                entityDo.setUpdateTime(DateUtil.getCommonDate());
            }

            ticketNotifyConfigDaoMapper.insert(entityDo);

            idList.add(entityDo.getTicketNotifyConfigId());
        }
        return idList;
    }

    @Override
    public TicketNotifyConfig findNotifyConfigByIdEvent(String ticketConfigId, Integer event) {
        TicketNotifyConfigDO ticketNotifyConfigDO = ticketNotifyConfigDaoMapper
                .findByTicketConfigIdAndEvent(ticketConfigId, event);

        return TicketNotifyConfigConverter.doToDto(ticketNotifyConfigDO);
    }

    @Override
    public TicketNotifyConfig updateNotifyConfig(String id, TicketNotifyConfig entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketNotifyConfigDO entityDo = TicketNotifyConfigConverter.dtoToDo(entity);

        if (Objects.isNull(entityDo)) {
            return null;
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        entityDo.setUpdateTime(DateUtil.getCommonDate());

        ticketNotifyConfigDaoMapper.updateByTicketNotifyConfigId(entityDo);

        return TicketNotifyConfigConverter.doToDto(entityDo);
    }
}
